package se.miun.dt156g.antonsskafferibestallning;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.ListView;

public class DrinksActivity extends Activity implements OnClickListener{

	private int tableNo;

	private ProgressDialog pDialog;

	private static final String READ_URL = "http://pellos.no-ip.org/android/beverage.php";

	private static final String TAG_POSTS = "posts";

	private static final String TAG_DRINK = "beverage";

	private JSONArray drinksMenu = null;

	private ArrayList<String> drinksMenuList;
	HashMap<String, Integer> noOfDrinks = new HashMap<String, Integer>();
	
	private ListView drinksListView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_drinks);

		Bundle extras = getIntent().getExtras();
		tableNo = extras.getInt("TableNo");
		
		drinksListView = (ListView)findViewById(R.id.drinkListView);

		Button nextButton = (Button)findViewById(R.id.drinksNextButton);
		nextButton.setOnClickListener(this);
		
		new LoadComments().execute();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.drinks, menu);
		return true;
	}

	public void updateJSONdata() {

		drinksMenuList = new ArrayList<String>();

		JSONParser jParser = new JSONParser();

		JSONObject json = jParser.getJSONFromUrl(READ_URL);

		try {

			drinksMenu = json.getJSONArray(TAG_POSTS);

			for (int i = 0; i < drinksMenu.length(); i++) {
				JSONObject c = drinksMenu.getJSONObject(i);

				drinksMenuList.add(c.getString(TAG_DRINK));
				noOfDrinks.put(c.getString(TAG_DRINK), 0);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

	private void updateList() {
		
		MultiSourceAdapter adapter = new MultiSourceAdapter(getApplicationContext(), drinksMenuList, noOfDrinks);
		
		drinksListView.setAdapter(adapter);
		
		drinksListView.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> parent, View view,
					int position, long id) {
				noOfDrinks.put(drinksMenuList.get(position),
						noOfDrinks.get(drinksMenuList.get(position)) - 1);
				updateList();
				return true;
				
			}
			
		});
		drinksListView.setOnItemClickListener(new OnItemClickListener() {
			
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {

				noOfDrinks.put(drinksMenuList.get(position),
						noOfDrinks.get(drinksMenuList.get(position)) + 1);
				updateList();
			}
		});
	}

	public class LoadComments extends AsyncTask<Void, Void, Boolean> {

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			pDialog = new ProgressDialog(DrinksActivity.this);
			pDialog.setMessage("Laddar meny...");
			pDialog.setIndeterminate(false);
			pDialog.setCancelable(true);
			pDialog.show();
		}

		@Override
		protected Boolean doInBackground(Void... arg0) {
			updateJSONdata();
			return null;
		}

		@Override
		protected void onPostExecute(Boolean result) {
			super.onPostExecute(result);
			pDialog.dismiss();
			updateList();
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.drinksNextButton:
			Intent startApetizerActivityIntent = new Intent(this, ApetizerActivity.class);
			startApetizerActivityIntent.putExtra("arraylist", drinksMenuList);
			startApetizerActivityIntent.putExtra("hashmap", noOfDrinks);
			startApetizerActivityIntent.putExtra("tableno", tableNo);
			startActivity(startApetizerActivityIntent);
			finish();
			break;
		}	
	}
}